<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Document</title>
  </head>
  <body></body>
  <script>
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var thirdMax = function(nums) {
      nums = [...new Set(nums)].sort((a, b) => {
        return b - a;
      });
      return nums[2] === undefined ? nums[0] : nums[2];
    };
    console.log(thirdMax([3, 2, 0, 1, 8]));
  </script>
</html>
